Use the code below to re-define the standardize() function and to create a vector with standardized numbers.

standardize <- function (x) {
  (x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE)
}

standardized_vector <-
  standardize(sample(1:100, 30))

1

Loop through each element of the vector and add the number pi using a for()-loop.
There’s a pre-defined value in R for pi. Just type pi and you’ll receive 3.141593. Also, you need to wrap everything within the print() function.
for (number in standardized_vector) {
  print(number + pi)
}
## [1] 4.112771
## [1] 2.907926
## [1] 1.410996
## [1] 3.528604
## [1] 3.126988
## [1] 2.068185
## [1] 2.214227
## [1] 3.492093
## [1] 2.141206
## [1] 3.27303
## [1] 2.798394
## [1] 2.50631
## [1] 4.76996
## [1] 1.958653
## [1] 2.542821
## [1] 3.053968
## [1] 3.419072
## [1] 1.264954
## [1] 4.660428
## [1] 2.323758
## [1] 4.295324
## [1] 1.812611
## [1] 4.477876
## [1] 3.930219
## [1] 3.601625
## [1] 4.623918
## [1] 3.747667
## [1] 2.360269
## [1] 4.149282
## [1] 3.674646

2

Repeat the previous exercise but this time the results should be printed all at once in a vector. You would need the sapply() function for that.
You don’t need the print() function anymore.
sapply(standardized_vector, function (number) {
  number + pi
}) 
##  [1] 4.112771 2.907926 1.410996 3.528604 3.126988 2.068185 2.214227 3.492093 2.141206 3.273030 2.798394 2.506310 4.769960 1.958653 2.542821 3.053968
## [17] 3.419072 1.264954 4.660428 2.323758 4.295324 1.812611 4.477876 3.930219 3.601625 4.623918 3.747667 2.360269 4.149282 3.674646

3

Now, we want to have the same output as a list. What’s your approach on doing this?
You can either use lapply() or purrr::map.
library(purrr)

lapply(standardized_vector, function (number) {
  number + pi
}) 
## [[1]]
## [1] 4.112771
## 
## [[2]]
## [1] 2.907926
## 
## [[3]]
## [1] 1.410996
## 
## [[4]]
## [1] 3.528604
## 
## [[5]]
## [1] 3.126988
## 
## [[6]]
## [1] 2.068185
## 
## [[7]]
## [1] 2.214227
## 
## [[8]]
## [1] 3.492093
## 
## [[9]]
## [1] 2.141206
## 
## [[10]]
## [1] 3.27303
## 
## [[11]]
## [1] 2.798394
## 
## [[12]]
## [1] 2.50631
## 
## [[13]]
## [1] 4.76996
## 
## [[14]]
## [1] 1.958653
## 
## [[15]]
## [1] 2.542821
## 
## [[16]]
## [1] 3.053968
## 
## [[17]]
## [1] 3.419072
## 
## [[18]]
## [1] 1.264954
## 
## [[19]]
## [1] 4.660428
## 
## [[20]]
## [1] 2.323758
## 
## [[21]]
## [1] 4.295324
## 
## [[22]]
## [1] 1.812611
## 
## [[23]]
## [1] 4.477876
## 
## [[24]]
## [1] 3.930219
## 
## [[25]]
## [1] 3.601625
## 
## [[26]]
## [1] 4.623918
## 
## [[27]]
## [1] 3.747667
## 
## [[28]]
## [1] 2.360269
## 
## [[29]]
## [1] 4.149282
## 
## [[30]]
## [1] 3.674646
standardized_vector %>% 
  map(~.x + pi)
## [[1]]
## [1] 4.112771
## 
## [[2]]
## [1] 2.907926
## 
## [[3]]
## [1] 1.410996
## 
## [[4]]
## [1] 3.528604
## 
## [[5]]
## [1] 3.126988
## 
## [[6]]
## [1] 2.068185
## 
## [[7]]
## [1] 2.214227
## 
## [[8]]
## [1] 3.492093
## 
## [[9]]
## [1] 2.141206
## 
## [[10]]
## [1] 3.27303
## 
## [[11]]
## [1] 2.798394
## 
## [[12]]
## [1] 2.50631
## 
## [[13]]
## [1] 4.76996
## 
## [[14]]
## [1] 1.958653
## 
## [[15]]
## [1] 2.542821
## 
## [[16]]
## [1] 3.053968
## 
## [[17]]
## [1] 3.419072
## 
## [[18]]
## [1] 1.264954
## 
## [[19]]
## [1] 4.660428
## 
## [[20]]
## [1] 2.323758
## 
## [[21]]
## [1] 4.295324
## 
## [[22]]
## [1] 1.812611
## 
## [[23]]
## [1] 4.477876
## 
## [[24]]
## [1] 3.930219
## 
## [[25]]
## [1] 3.601625
## 
## [[26]]
## [1] 4.623918
## 
## [[27]]
## [1] 3.747667
## 
## [[28]]
## [1] 2.360269
## 
## [[29]]
## [1] 4.149282
## 
## [[30]]
## [1] 3.674646